const path = require('path')
const webpack = require('webpack')
//css与js分开打包(不使用webpack，loader会把css文件打包进js文件中，即项目css样式将会是通过js文件生成，会带来延迟)
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HappyPack = require('happypack')
const os = require('os')
// webpack多核心优化
const happyThreadPool = HappyPack.ThreadPool({size: os.cpus().length});

function pathJoin(relatedPath) {
    return path.join(__dirname, relatedPath);
}

const webpackConfigBase = {
    // 入口文件
    entry: {
        client: pathJoin('../app/index.js')
    },
    // 出口文件
    output: {
        path: pathJoin('../dist'),// 打包后的文件目录，绝对路径
        filename: '[name].[hash:4].js',// 打包后的文件名，添加hash，生成4位的hash串，防止文件缓存
        chunkFilename: 'chunks/[name].[hash:4].js'// 打包被入口文件异步加载的间接的js文件
    },
    resolve: {
        extensions: ['.js', '.json'],// 默认后缀查找顺序
        alias: {//文件引入别名配置
            '@app': pathJoin('../app'),
            '@actions': pathJoin('../app/redux/actions'),
            '@reducers': pathJoin('../app/redux/reducers'),
            '@apis': pathJoin('../app/apis'),
            '@components': pathJoin('../app/components'),
            '@configs': pathJoin('../app/config'),
            '@config': pathJoin('../app/config/config.js'),
            '@ajax': pathJoin('../app/apis/ajax.js'),
            '@utils': pathJoin('../app/utils'),
            '@reg': pathJoin('../app/config/reg.config.js'),
            '@images': pathJoin('../app/images'),
            '@middleware': pathJoin('../app/middleware'),
            '@pages': pathJoin('../app/pages'),
            '@style': pathJoin('../app/style'),
            '@tableList': pathJoin('../app/components/tableList/tableList.js'),
        }
    },
    resolveLoader: {
        moduleExtensions: ['-loader']
    },
    // 处理对应模块
    module: {
        rules: [
            {
                test: /\.js[x]?$/,
                exclude: /node_modules/,
                include: [pathJoin('../app')],
                loader: 'happypack/loader?id=happyBabel'// 将.js文件的处理交给id为happyBabel的HappyPack实例执行
            },
            {
                test: /\.(css|less)$/,
                // exclude:/node_modules/,
                include: [
                    pathJoin('../app/style'),
                    pathJoin('../app/components'),
                    pathJoin('../node_modules/antd'),
                    pathJoin('../node_modules/draft-js'),
                ],
                loader: ExtractTextPlugin.extract({fallback: 'style', use: 'happypack/loader?id=happyStyle'}),
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                exclude: /node_modules/,
                include: [pathJoin('../app/images')],
                loader: 'url',
                options: {
                    limit: 8192,
                    name: '../images/[name].[ext]'
                }
            },
            {
                test: /\.(woff|eot|ttf|svg|gif)$/,
                loader: 'url',
                options: {
                    limit: 8192,
                    name: 'font/[name].[hash:4].[ext]'
                }
            }
        ]
    },
    // 对应的插件
    plugins: [
        new HappyPack({
            id:'happyBabel',// 通过id来标识需要happypack来处理的文件
            loaders:[ // loader配置
                {loader:'babel?cacheDirectory=true'}
            ],
            threadPool:happyThreadPool,// 进程池配置，多个HappyPack共享同一个线程池，使用其中的子程序处理任务，防止资源占用过多
            verbose:true // 允许HappyPack输出日志
        }),
        new HappyPack({
            id:'happyStyle',// 通过id来标识需要happypack来处理的文件
            loaders: [ 'css-loader?sourceMap=true', 'less-loader?sourceMap=true' ],
            threadPool:happyThreadPool,// 进程池配置，多个HappyPack共享同一个线程池，使用其中的子程序处理任务，防止资源占用过多
            verbose:true // 允许HappyPack输出日志
        }),
        new ExtractTextPlugin('./style/style.[hash:4].css'),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common', // 入口文件名
            filename: './js/common.[hash:4].js', // 打包后的文件名
            minChunks: function (module, count) {
                return module.resource &&
                    /\.js$/.test(module.resource) &&
                    module.resource.indexOf(pathJoin('../node_modules')) === 0
            }
        }),
        new webpack.optimize.CommonsChunkPlugin({
            async: 'async-common',
            minChunks: 3,
        }),
    ]
}
module.exports =  webpackConfigBase
